#include "include.h"
#include "bsp_uart.h"

#define UART_EXAMPLE_EN   1    ///普通uart示例程序

#if  UART_EXAMPLE_EN
#define UART_TX  4  //pa7_tx (G1)
#define UART_RX  3	//pa6_rx (G1)
#define MAX_WRITE_INDEX 16
u8 uart_buf[MAX_WRITE_INDEX][256] AT(.uart_rx.buf);
func_uart_t uart_info ;

///发送数据
AT(.com_text.uart)
void uart1_putc(char ch)
{
    while(!(UART1CON & BIT(8)));
    UART1DATA = ch;
}

///打印字符串
AT(.com_text.uart)
void uart1_puts(const char *str)
{
    while(*str){
        uart1_putc(*str++);
    }
}

AT(.text.uart)
func_uart_t* get_uart_info(void)
{
	return &uart_info;
}
AT(.com_text.uart)
u8* get_uart_buf(u8 index)
{
	return &uart_buf[index][0];
}
AT(.text.uart)
void reset_uart_info(void)
{
	memset(&uart_info,0x0,sizeof(func_uart_t));
}


///串口中断处理
AT(.com_text.uart)
void uart1_isr(void)
{
    if(UART1CON & BIT(9)){      //接收完成
        UART1CPND |= BIT(9);
        //需要把数据保存起来
        uart_buf[uart_info.w_index][uart_info.uart_len] = UART1DATA;
		uart_info.uart_len++;
		if(uart_info.uart_len>=256)
		{
			uart_info.uart_len=0;
			uart_info.w_index++;
			uart_info.w_block_index++;
			if(uart_info.w_index>=MAX_WRITE_INDEX)
			{
				uart_info.w_index=0;
			}
		}
		//printf("uart d:%d\n",uart_buf[0]);
    }
}

///串口初始化
AT(.text.at_mode)
#if 1
void uart1_init(u32 baud)
{
    u32 baud_cfg;
    //PA3_RX,PA4_TX(G2)
    GPIOAFEN |= BIT(UART_RX);
    GPIOADIR |= BIT(UART_RX);
    GPIOAPU |= BIT(UART_RX);
    GPIOADE |= BIT(UART_RX);
    //mapping uart1(tx/rx) -> G2
    FUNCMCON0 |= (0xf << 28);
    FUNCMCON0 |= (0x2 << 28);

    CLKGAT0 |= BIT(14);                                 //enable uart1 clk

    UART1CON = 0;
    UART1CON |= BIT(5);                                 //clock src select uart_inc
    CLKCON1 |= BIT(14);                                 //uart_inc select x26m_clkdiv2
    baud_cfg = ((26000000/2+ baud/2)/baud) - 1;         //baud_cfg=(串口时钟/波特率)-1; 四舍五入
    UART1BAUD = (baud_cfg << 16)|baud_cfg;

    UART1CON |= (BIT(0)|BIT(2)|BIT(7));                 //使能uart,接收中断

    register_isr(IRQ_UART_VECTOR, uart1_isr);
    PICPR &= ~BIT(IRQ_UART_VECTOR);
    PICEN |= BIT(IRQ_UART_VECTOR);
}
void uart1_exit(void)
{
	UART1CON=0;
	PICPR &= ~BIT(IRQ_UART_VECTOR);
	PICEN &= ~BIT(IRQ_UART_VECTOR);
	GPIOAFEN &= ~BIT(UART_RX);
	GPIOADE &= ~BIT(UART_RX);
	GPIOADIR &= ~BIT(UART_RX);
}

#else
void uart1_init(u32 baud)
{
    u32 baud_cfg;
    //PA3_RX,PA4_TX(G2)
    GPIOAFEN |= BIT(UART_RX)|BIT(UART_TX);
    GPIOADIR |= BIT(UART_RX);
    GPIOADIR &= ~BIT(UART_TX);
    GPIOAPU |= BIT(UART_RX)|BIT(UART_TX);
    GPIOADE |= BIT(UART_RX)|BIT(UART_TX);
    //mapping uart1(tx/rx) -> G2
    FUNCMCON0 |= (0xf << 28)|(0xf << 24);
    FUNCMCON0 |= (0x2 << 28)|(0x2 << 24);

    CLKGAT0 |= BIT(14);                                 //enable uart1 clk

    UART1CON = 0;
    UART1CON |= BIT(5);                                 //clock src select uart_inc
    CLKCON1 |= BIT(14);                                 //uart_inc select x26m_clkdiv2
    baud_cfg = ((26000000/2+ baud/2)/baud) - 1;         //baud_cfg=(串口时钟/波特率)-1; 四舍五入
    UART1BAUD = (baud_cfg << 16)|baud_cfg;

    UART1CON |= (BIT(0)|BIT(2)|BIT(7));                 //使能uart,接收中断

    register_isr(IRQ_UART_VECTOR, uart1_isr);
    PICPR &= ~BIT(IRQ_UART_VECTOR);
    PICEN |= BIT(IRQ_UART_VECTOR);
}
#endif
#endif // UART_EXAMPLE_EN
